<?php

namespace Biz\service\brand;

use Biz\module\Classes\adm\ContentSearch as ContentBrand;
use Doraemon\model\Member;
use Doraemon\model\Photos;
use Doraemon\service\Brand\BrandContentSearch;
use Doraemon\service\es\EsBrandContent;
use Doraemon\tools\BlockType;

class  SearchSchedule
{
    public static function search(ContentBrand $contentBrand)
    {
        $params = array(
            "page" => $contentBrand->page,
            "pagesize" => $contentBrand->page_size,
            "sort" => $contentBrand->sort,
            "fl" => $contentBrand->fl,
        );

        // 品牌id
        if ($contentBrand->brand_id !== null) {
            $params["brand_id"] = $contentBrand->brand_id;
        }

        // 品牌ids
        if ($contentBrand->brand_ids) {
            $params["brand_ids"] = $contentBrand->brand_ids;
        }

        // 搜索词
        if ($contentBrand->keyword) {
            $params["keyword"] = $contentBrand->keyword;
        }

        //  内容类型
        if (is_array($contentBrand->obj_type) && $contentBrand->obj_type) {
            $params["obj_type"] = implode(",", $contentBrand->obj_type);
        }

        // 发布者
        if ($contentBrand->is_owner === true) {
            $params["is_owner"] = 1;
        } else if ($contentBrand->is_owner === false) {
            $params["is_owner"] = 0;
        }

        // 发布日期
        // 开始
        if (isset($contentBrand->publish_time_range["start"]) && $contentBrand->publish_time_range["start"]) {
            $params["publish_time_start"] = $contentBrand->publish_time_range["start"];
        }

        // 结束
        if (isset($contentBrand->publish_time_range["end"]) && $contentBrand->publish_time_range["end"]) {
            $params["publish_time_end"] = $contentBrand->publish_time_range["end"];
        }

        // 收录日期
        // 开始
        if (isset($contentBrand->claim_time_range["start"]) && $contentBrand->claim_time_range["start"]) {
            $params["claim_time_start"] = $contentBrand->claim_time_range["start"];
        }

        // 结束
        if (isset($contentBrand->claim_time_range["end"]) && $contentBrand->claim_time_range["end"]) {
            $params["claim_time_end"] = $contentBrand->claim_time_range["end"];
        }

        //  tab
        if ($contentBrand->tab) {
            switch ($contentBrand->tab) {
                case 1 :
                    $params["claim_content_status"] = 1;
                    break;
                case 2 :
                    $params["is_selected"] = 1;
                    break;
                case 3 :
                    $params["claim_content_status"] = 3;
                    break;
            }
        }

        // claim_content_status
        if ($contentBrand->claim_content_status !== null) {
            $params["claim_content_status"] = $contentBrand->claim_content_status;
        }

        // filter_audit_status
        if (is_array($contentBrand->filter_audit_status) && $contentBrand->filter_audit_status) {
            $params["filter_audit_status"] = implode(",", $contentBrand->filter_audit_status);
        }

        // sort way
        if ($contentBrand->desc) {
            $params["sort_way"] = $contentBrand->desc;
        }

        // filter_audit_status
        if (is_array($contentBrand->claim_type) && $contentBrand->claim_type) {
            $params["claim_type"] = implode(",", $contentBrand->claim_type);
        }

        // wiki basic id
        if ($contentBrand->wiki_id) {
            $params["wiki_basic_id"] = $contentBrand->wiki_id;
        }
        // wiki title
        if ($contentBrand->wiki_title) {
            $params["wiki_title"] = $contentBrand->wiki_title;
        }

        // 置顶
        if ($contentBrand->is_top && $contentBrand->is_top == 1) {
            $params["is_top"] = $contentBrand->is_top;
        }

        // 品牌ids
        if (is_array($contentBrand->brand_ids) && $contentBrand->brand_ids) {
            $params["brand_ids"] = implode(",", $contentBrand->brand_ids);
        }

        // current_time 当前时间
        if ($contentBrand->current_time !== null && $contentBrand->current_time) {
            $params["current_time"] = $contentBrand->current_time;
        }

        // 检索词类型
        if (is_array($contentBrand->search_keywords) && $contentBrand->search_keywords) {
            $params["search_keywords"] = implode(",", $contentBrand->search_keywords);
        }

        // 过滤特定品牌uid
        if (is_array($contentBrand->filter_owner_uid) && $contentBrand->filter_owner_uid) {
            $params["filter_owner_uid"] = implode(",", $contentBrand->filter_owner_uid);
        }

        $rs = EsBrandContent::search($params);

        $return = array(
            "total" => 0,
            "is_over" => 1,
            "list" => array()
        );

        if (is_array($rs) && $rs) {
            $return["total"] = $rs["total"];
            $return["list"] = array_values($rs["rows"]);
            $return["is_over"] = $contentBrand->page_size <= count($rs["rows"]) ? 0 : 1;
        }

        return $return;
    }

    /** 商家品牌后台 品牌认领 422
     * @param $brandId
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public static function waitClaimList($params)
    {
        $return = array(
            "total" => 0,
            "is_over" => 1,
            "list" => array()
        );

        if (!$params['brand_id']) {
            return $return;
        }
        $params["fl"] = "id,like,favorite,fans,comment,owner_uid,admin_score";
        $paramsObj = new  BrandContentSearch($params);

        $rs = self::handleWaitClaimList($paramsObj);

        if (is_array($rs) && $rs) {
            $return["total"] = $rs["total"];
            $return["list"] = $rs["rows"];
            $return["is_over"] = $params['page_size'] <= count($rs["rows"]) ? 0 : 1;
        }

        return $return;
    }


    /** 商家品牌后台 品牌认领 处理条件
     * @param $brandId
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public static function handleWaitClaimList(BrandContentSearch $paramsObj)
    {
        $params = array(
            "page" => $paramsObj->page,
            "pagesize" => $paramsObj->page_size,
            "brand_id" => $paramsObj->brand_id,
        );

        // 排序方式
        if ($paramsObj->sort_type !== null) {
            switch ($paramsObj->sort_type) {
                case BrandContentSearch::SORT_TYPE_ZAN:
                    $params["sort"] = "like";
                    break;
                case BrandContentSearch::SORT_TYPE_FAV:
                    $params["sort"] = "favorite";
                    break;
                case BrandContentSearch::SORT_TYPE_COM:
                    $params["sort"] = "comment";
                    break;
                case BrandContentSearch::SORT_TYPE_FAN:
                    $params["sort"] = "fans";
                    break;
                case BrandContentSearch::SORT_TYPE_TIME_DESC:
                    $params["sort"] = "addtime";
                    break;
                case BrandContentSearch::SORT_TYPE_TIME_ASC:
                    $params["sort"] = "addtime";
                    $params["sort_type"] = "asc";
                    break;
                case BrandContentSearch::SORT_TYPE_AI:
                    $params["sort"] = "addtime";
                    $params["sort_type"] = "desc";
                    //一年内的优质、精选内容
                    $params['addtime_start'] = $paramsObj->time_range['start'];
                    $params['addtime_end'] = $paramsObj->time_range['end'];
                    $params['admin_score_range_start'] = $paramsObj->admin_score_range['min'];
                    $params['admin_score_range_end'] = $paramsObj->admin_score_range['max'];
                    break;
            }
        }

        if ($paramsObj->admin_score_range) {
            if (isset($paramsObj->admin_score_range['min'])) $params['admin_score_range_start'] = $paramsObj->admin_score_range['min'];
            if (isset($paramsObj->admin_score_range['max'])) $params['admin_score_range_end'] = $paramsObj->admin_score_range['max'];
        }

        if ($paramsObj->fl !== null) {
            $params["fl"] = $paramsObj->fl;
        }

        if ($paramsObj->brand_name !== null) {
            $params["brand_name"] = $paramsObj->brand_name;
        }

        if (is_array($paramsObj->brand_alias) && $paramsObj->brand_alias) {
            $params["brand_alias"] = implode(",", $paramsObj->brand_alias);
        }

        if (is_array($paramsObj->search_keywords) && $paramsObj->search_keywords) {
            $params["search_keywords"] = implode(",", $paramsObj->search_keywords);
        }

        // 商品类型
        if ($paramsObj->goods_type !== null) {
            switch ($paramsObj->goods_type) {
                case BrandContentSearch::GOODS_TYPE_SAME:
                    $params["is_similar_goods"] = 1;
                    break;
                case BrandContentSearch::GOODS_TYPE_DIFF:
                    $params["is_similar_goods"] = 0;
                    break;
            }
        }

        // 内容类型
        if ($paramsObj->content_type !== null) {
            switch ($paramsObj->content_type) {
                case BrandContentSearch::CONTENT_TYPE_PIC:
                    $params["obj_type"] = BlockType::TYPE_PHOTO;
                    $params["show_type"] = Photos::SHOW_TYPE_PHOTO;
                    break;
                case BrandContentSearch::CONTENT_TYPE_VIDIO:
                    $params["obj_type"] = BlockType::TYPE_PHOTO;
                    $params["show_type"] = Photos::SHOW_TYPE_VIDEO;
                    break;
                case BrandContentSearch::CONTENT_TYPE_NOTE:
                    $params["obj_type"] = BlockType::TYPE_BLANK;
                    break;
            }
        }

        // 用户类型
        if (is_array($paramsObj->user_type) && $paramsObj->user_type) {
            $userTypes = array();
            foreach ($paramsObj->user_type as $userType) {
                switch ($userType) {
                    case BrandContentSearch::USER_TYPE_USER:
                        array_push($userTypes, Member::USER_COMMON);
                        break;
                    case BrandContentSearch::USER_TYPE_DESIGNER:
                        array_push($userTypes, Member::AUTH_USER_DESIGNER);
                        break;
                    case BrandContentSearch::USER_TYPE_BRAND:
                        array_push($userTypes, Member::AUTH_USER_BRAND);
                        break;
                }
            }

            if (is_array($userTypes) && $userTypes) {
                $params["user_type"] = implode(",", $userTypes);
            }
        }

        $rs = EsBrandContent::waitClaimList($params);

        return $rs;
    }
}